﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL.Entity;

namespace DAL.Persistencia
{
    public class FileTransfersDAL
    {

        private NewTierraEntities Con;

        public FileTransfersDAL()
        {
            Con = new NewTierraEntities();
        }

        public List<File_Transfers> ListarTodosNew(int IdFile)
        {
            try
            {
                return Con.File_Transfers.Where(f => f.File_id == IdFile && f.Range_id == null).ToList();
            }
            catch
            {
                throw;
            }
        }

        public List<File_Transfers> ListarTodos(int IdFile)
        {
            try
            {
                return Con.File_Transfers.Where(f => f.File_id == IdFile &&
                                                     f.Range_id == null).ToList();
            }
            catch
            {
                throw;
            }
        }

        public List<File_Transfers> ListarTodosRangeZero(int IdFile)
        {
            try
            {
                return Con.File_Transfers.Where(f => f.File_id == IdFile &&
                                                     f.Range_id != null).ToList();
            }
            catch
            {
                throw;
            }
        }

        public List<File_Transfers> ListarTodosOpt(int IdFile, int OptQuote)
        {
            try
            {
                return Con.File_Transfers.Where(f => f.File_id == IdFile &&
                                                     f.OptQuote == OptQuote).ToList();
            }
            catch
            {
                throw;
            }
        }

        public List<File_Transfers> ListarTodosPorOrdem(int IdFile, int Ordem)
        {
            try
            {
                return Con.File_Transfers.Where(f => f.File_id == IdFile &&
                                                     f.Ordem >= Ordem).ToList();
            }
            catch
            {
                throw;
            }
        }

        public List<File_Transfers> ListarTodos(int IdFile, int IdSupplier)
        {
            try
            {
                return Con.File_Transfers.Where(f => f.File_id == IdFile &&
                                                   f.S_id == IdSupplier).ToList();
            }
            catch
            {
                throw;
            }
        }

        public List<File_Transfers> ListarTodos(string trfNome)
        {
            try
            {
                return Con.File_Transfers.Where(f => f.Transf_nome.Equals(trfNome)).ToList();
            }
            catch
            {
                throw;
            }
        }

        public object ListarTodosSuppliers(int IdFile)
        {
            try
            {
                return (from flt in Con.File_Transfers
                        where flt.File_id == IdFile
                        select new
                        {
                            flt.S_nome
                        }).ToList().Distinct();
            }
            catch
            {
                throw;
            }
        }

        public void Salvar(File_Transfers f)
        {
            try
            {
                Con.AddToFile_Transfers(f);
                Con.SaveChanges();
            }
            catch
            {
                throw;
            }
        }

        public File_Transfers ObterPorId(int IdFileTransf)
        {
            try
            {
                return Con.File_Transfers.Where(f => f.File_Transf_id == IdFileTransf).SingleOrDefault();
            }
            catch
            {
                throw;
            }
        }

        public void Excluir(File_Transfers f)
        {
            try
            {
                Con.DeleteObject(f);
                Con.SaveChanges();
            }
            catch
            {
                throw;
            }
        }

        public void Atualizar(File_Transfers novo)
        {
            try
            {
                File_Transfers antigo = ObterPorId(novo.File_Transf_id);

                antigo.Qtd_Transf = novo.Qtd_Transf;
                antigo.markup = novo.markup;
                antigo.markupNet = novo.markupNet;
                antigo.descontoNet = novo.descontoNet;
                antigo.desconto = novo.desconto;
                antigo.Transf_valor = novo.Transf_valor;
                antigo.Transf_venda = novo.Transf_venda;
                antigo.Transf_vendaNet = novo.Transf_vendaNet;
                antigo.Transf_total = novo.Transf_total;
                antigo.UpdVenda = novo.UpdVenda;

                Con.SaveChanges();
            }
            catch
            {
                throw;
            }
        }

        public void AtualizarVenda(File_Transfers novo)
        {
            try
            {
                File_Transfers antigo = ObterPorId(novo.File_Transf_id);

                antigo.S_merc_tarif_vendaNet = novo.S_merc_tarif_vendaNet;
                antigo.markup = novo.markup;
                antigo.Transf_venda = novo.Transf_venda;                
                antigo.Transf_total = novo.Transf_total;
                antigo.UpdVenda = true;
                
                Con.SaveChanges();
            }
            catch
            {
                throw;
            }
        }

        public void AtualizarVendaTotal(File_Transfers novo)
        {
            try
            {
                File_Transfers antigo = ObterPorId(novo.File_Transf_id);

                antigo.Transf_venda = novo.Transf_venda;
                //antigo.Transf_vendaNet = novo.Transf_vendaNet;
                antigo.Transf_total = novo.Transf_total;

                Con.SaveChanges();
            }
            catch
            {
                throw;
            }
        }

        public void AtualizarStatus(File_Transfers novo)
        {
            try
            {
                File_Transfers antigo = ObterPorId(novo.File_Transf_id);

                antigo.Status = novo.Status;

                Con.SaveChanges();
            }
            catch
            {
                throw;
            }
        }

        public void AtualizarBulk(File_Transfers novo)
        {
            try
            {
                File_Transfers antigo = ObterPorId(novo.File_Transf_id);

                antigo.TBulk = novo.TBulk;

                Con.SaveChanges();
            }
            catch
            {
                throw;
            }
        }

        public void AtualizarFlight(File_Transfers novo)
        {
            try
            {
                File_Transfers antigo = ObterPorId(novo.File_Transf_id);

                antigo.Flights_id = novo.Flights_id;

                Con.SaveChanges();
            }
            catch
            {
                throw;
            }
        }

        public void AtualizarPaxs(File_Transfers novo)
        {
            try
            {
                File_Transfers antigo = ObterPorId(novo.File_Transf_id);

                antigo.Qtd_Transf = novo.Qtd_Transf;
                antigo.Paxs = novo.Paxs;
                antigo.Paying_Pax = novo.Paying_Pax;
                antigo.Supp_Paying_Pax = novo.Supp_Paying_Pax;

                Con.SaveChanges();
            }
            catch
            {
                throw;
            }
        }

        public void AtualizarValorTotal(File_Transfers novo)
        {
            try
            {
                File_Transfers antigo = ObterPorId(novo.File_Transf_id);

                antigo.Transf_valor = novo.Transf_valor;
                antigo.Transf_venda = novo.Transf_venda;
                antigo.Transf_total = novo.Transf_total;

                Con.SaveChanges();
            }
            catch 
            {                
                throw;
            }
        }

        public void AtualizarOrdem(File_Transfers novo)
        {
            try
            {
                File_Transfers antigo = ObterPorId(novo.File_Transf_id);

                antigo.Ordem = novo.Ordem;

                Con.SaveChanges();
            }
            catch
            {
                throw;
            }
        }

        public void AtualizarNome(File_Transfers novo)
        {
            try
            {
                File_Transfers antigo = ObterPorId(novo.File_Transf_id);

                antigo.Transf_nome = novo.Transf_nome;

                Con.SaveChanges();
            }
            catch
            {
                throw;
            }
        }

        public void AtualizarDatas(File_Transfers novo)
        {
            try
            {
                File_Transfers antigo = ObterPorId(novo.File_Transf_id);

                antigo.Data_From = novo.Data_From;
                antigo.Data_To = novo.Data_To;

                Con.SaveChanges();
            }
            catch
            {
                throw;
            }
        }

        public int RetornaUltimaOrdem(int IdFile)
        {
            try
            {
                var item = Con.File_Transfers.Where(f => f != null && f.File_id == IdFile).Max(f => f.Ordem);

                if (item != null)
                {
                    return Convert.ToInt32(item);
                }
                else
                {
                    return 0;
                }

            }
            catch
            {
                throw;
            }
        }

        public void AtualizarRange(File_Transfers novo)
        {
            try
            {
                File_Transfers antigo = ObterPorId(novo.File_Transf_id);

                antigo.Range_id = novo.Range_id;

                Con.SaveChanges();
            }
            catch
            {
                throw;
            }
        }

        public void AtualizarNew(File_Transfers novo)
        {
            try
            {
                File_Transfers antigo = ObterPorId(novo.File_Transf_id);

                antigo.Qtd_Transf = novo.Qtd_Transf;                   
                antigo.Paying_Pax = novo.Paying_Pax;
                antigo.Supp_Paying_Pax = novo.Supp_Paying_Pax;
                antigo.Paxs = novo.Paxs;
                Con.SaveChanges();
            }
            catch
            {
                throw;
            }
        }

    }
}
